<?php
namespace app\zhongcan\controller;
use app\zhongcan\model\NewsModel;
use app\zhongcan\validate\NewsValidate;
use cmf\controller\AdminBaseController;

class NewsController extends AdminBaseController{
    //列表
    public function index()
    {
       $keyword=input('keyword','');
      // echo  $keyword;
        //存放条件
        $where=[];
        if($keyword){
            $where[]=['title|author','like','%'.$keyword.'%'];
        }
       $list= NewsModel::where($where)
           ->order('list_order','desc')
           ->paginate(3);
       $this->assign('news',$list);
        return $this->fetch('index',[
            'news'=>$list
        ]);
    }
    //显示添加页面
    public function add(){
        return $this->fetch('add');
    }
    public function addPost(){
        //接收
       $data=input('post.');
//       $content=input('content');
        $content= $data['content'];
//        halt( $data['content']);
//        富文本处理
       // 一次处理，还原解密
       $content1= htmlspecialchars_decode($content);
       //二次处理加密，替换编辑器内容中的文件地址,主要是处理图片地址
        $content2=cmf_replace_content_file_url($content1,true);
       // 三次处理 还原
        $content3=htmlspecialchars($content2);
//        $data['content']= $content3;
       $content= $content3;
        $v=new NewsValidate();
        $r=$v->scene('add')->check($data);
        if(!$r){
            $this->error($v->getError());
        }
        $res=NewsModel::create($data);
        if($res){
            return json(['code' => 1,'msg' => '保存成功','data' => null]);
        }else{
            return json(['code' => 0,'msg' => '保存失败','data' => null]);
        }
    }
    //显示编辑页面
    public function edit(){
        $id=input('id/d');
        $news = NewsModel::find($id);
       $content1= htmlspecialchars_decode(  $news['content']);
       $content2=cmf_replace_content_file_url($content1,true);
       $news['content']= $content2;

       $news['content'];
        return $this->fetch('edit',[
            'news'=>$news
        ]);

    }
    public function editPost(){
        //接收
        $data=input('post.');
        $content=input('content');
//      $data['content'];
//        halt( $data['content']);
//        富文本处理
        // 一次处理，还原解密
        $content1= htmlspecialchars_decode($content);
        //二次处理加密，替换编辑器内容中的文件地址,主要是处理图片地址
        $content2=cmf_replace_content_file_url($content1,true);
        // 三次处理 还原
        $content3=htmlspecialchars($content2);
//        $data['content']= $content3;
        $content= $content3;
        $v=new NewsValidate();
        $r=$v->scene('edit')->check($data);
        if(!$r){
            $this->error($v->getError());
        }
        //接受数据
        $news = NewsModel::find($data);
        if(!$news){
            $this->error('未查询到数据');
        }
        $news->title=$data['title'];
        $news->image=$data['image'];
        $news->author=$data['author'];
        $news->look_num=$data['look_num'];
        $news->content=$data['content'];

        //入库

        if($news->save()){
            $this->success('保存成功','news/index');
        }else{
            $this->error('保存失败','news/index');
        }
    }
    //删除
    public function del(){
        $id=input('id/d');
        $del=NewsModel::destroy( $id);
        if($del){
            $this->success('保存成功','news/index');
        }else{
            $this->error('保存失败','news/index');
        }
    }
    //排序
    public function order(){
       $order=parent::listOrders(new NewsModel());
        if($order){
            $this->success('排序成功','news/index');
        }else{
            $this->error('排序失败','news/index');
        }
    }
    //预览文章
    public function detail($id){
        $news = NewsModel::find($id);
        if (!$news) {
            $this->error('文章不存在');
        }
        $content = $news['content'];
        $content1 = htmlspecialchars_decode($content);
        $content2 = cmf_replace_content_file_url($content1, true);
        $article['content'] = $content2;


        $this->assign('news',$news);
        return $this->fetch('detail');
    }

    public function changeSwitch(){
        $id=input('id/d');
        $news = NewsModel::find($id);
        if($news['switch']==0){
           return $newswitch  = 1;
        }else{
            $newswitch  = 0;
        }
        $news->switch=$newswitch;
        if($news->save()){
            $this->success('状态切换成功','News/index');
        }else{
            $this->error('状态切换失败','News/index');
        }
    }
}